# encoding: utf-8
"""
@author: 夏洛
@QQ: 1972386194
@file: mains.py
"""
# encoding: utf-8

from flask import Flask, render_template, request, make_response
# pip install flask

'''

cookie 携带信息去访问服务器


'''


app = Flask(__name__)
from hashlib import md5

def md(content):
    m = md5()
    m.update(content.encode(encoding='utf-8'))
    str_md5 = m.hexdigest()
    return str_md5  # 32位


'''
md5 算法 不可逆

同一个加密的对象  生成的结果 一模一样

'''

def parse(tim,sign):
    sale = tim + 'feifei'
    token = md(sale)
    if sign == token:
        return True
    else:
        return False

@app.after_request
def ccc(res):
    res.set_cookie('name', 'xialuo')
    return res

@app.route('/')
def index():
   return render_template('ff.html')



import datetime,time
def delas(tp):
    # 拿服务器时间与前台传得时间做对比 相差10秒
    tamp = int(tp)
    now = round(int(time.time()))
    print(tamp,now,'调试')
    dela = datetime.datetime.fromtimestamp(now) - datetime.datetime.fromtimestamp(tamp)
    print(dela.total_seconds(),'时间戳')
    return dela.total_seconds()  # 返回时间差

@app.route('/api')
def detail():
    t = request.cookies.get('per')
    toekn = request.cookies.get('__zp_stoken__')
    print(t,toekn)
    if not all([t,toekn]):
        return '参数错误'
    scords = delas(t)
    if parse(t,toekn) and scords < 10:  # 时间差
        return render_template('demo.html')
    else:
        return '你是一个不要爱情得爬虫'

if __name__ == '__main__':
    app.run()
